<template>
  <div class="xtx-goods-page" v-if="goodsInfo.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">
          {{ goodsInfo.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsInfo.categories[0].id}`">
          {{ goodsInfo.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goodsInfo" />
          <!-- 规格组件 -->
          <GoodsSku :goods="goodsInfo" @changeSku="changeSku" />
          <!-- 数字框组件 -->
          <XtxNumBox v-model="count" showLabel />
          <XtxButton type="primary" style="margin-top: 20px" @click="onAddCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail :goods="goodsInfo" />
            <!-- 商品详情 -->
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'

const { goods, cartStore } = useStore()
const route = useRoute()
const { goodsInfo } = storeToRefs(goods)

watchEffect(() => {
  goods.resetGoodsInfo()
  goods.loadGoodsInfo(route.params.id as string)
})
// 保存skuId
const currentSkuId = ref('')
const changeSku = (skuId: string) => {
  currentSkuId.value = skuId
  const sku = goodsInfo.value.skus.find((item) => item.id === skuId)
  if (sku) {
    // 修改库存
    goodsInfo.value.inventory = sku.inventory
    // 修改价格
    goodsInfo.value.price = sku.price
    // 修改原价
    goodsInfo.value.oldPrice = sku.oldPrice
  }
}

const count = ref(1)

// 加入购物车
const onAddCart = async () => {
  // 判断是否是完整的sku
  if (!currentSkuId.value) {
    return Message.warning('请选择完整信息')
  }
  // 获取规格
  const sku = goodsInfo.value.skus.find(
    (item) => item.id === currentSkuId.value
  )
  const attrsText = sku?.specs
    .map((item) => `${item.name} ${item.valueName}`)
    .join(' ')

  await cartStore.addCartList({
    // 本地添加
    id: goodsInfo.value.id,
    name: goodsInfo.value.name,
    picture: goodsInfo.value.mainPictures[0],
    price: goodsInfo.value.price,
    count: count.value,
    skuId: currentSkuId.value,
    attrsText: attrsText || '',
    selected: true,
    nowPrice: goodsInfo.value.price,
    stock: goodsInfo.value.inventory,
    isEffective: true
  } as CartItem)

  Message.success('加入购物车成功')
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
